<template>
  <div class="app-container">
    <el-card>
      <!-- 搜索 -->
      <el-row :gutter="40">
        <el-form :inline="true" :model="listQuery" size="small">
          <el-form-item>
            <el-input
              placeholder="请输入内容"
              clearable
              v-model="listQuery.name"
              @clear="getDocTypeList"
            >
              <el-button
                slot="append"
                icon="el-icon-search"
                @click="getDocTypeList"
              >
              </el-button>
            </el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="small"
              @click="addDocType"
            >
              新增资料类型
            </el-button>
          </el-form-item>
        </el-form>
      </el-row>
      <!-- 列表 -->
      <el-table
        style="width: 100%"
        v-loading="listLoading"
        highlight-current-row
        border
        size="small"
        :data="docTypeList"
      >
        <el-table-column label="#" align="center" type="index" width="50">
        </el-table-column>
        <el-table-column prop="id" align="center" label="类型ID">
        </el-table-column>
        <el-table-column prop="name" align="center" label="类型名称">
        </el-table-column>
        <el-table-column align="center" label="操作" width="200px">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="handleEdit(scope.row)"
            >
              编辑
            </el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="handleDelete(scope.row.id)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 弹框 -->
    <el-dialog
      :title="edit ? '修改资料类型' : '新增资料类型'"
      :visible.sync="dialogVisible"
      width="50%"
      @closed="handleCloseDialog"
    >
      <el-form
        :model="addForm"
        :rules="addFormRules"
        ref="addFormRef"
        label-width="80px"
        label-position="right"
        style="width: 95%"
      >
        <el-form-item label="类型ID" prop="id">
          <el-input
            v-model="addForm.id"
            placeholder="类型ID"
            clearable
            :disabled="edit"
          />
        </el-form-item>
        <el-form-item label="类型名称" prop="name">
          <el-input v-model="addForm.name" placeholder="类型名称" clearable />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="edit ? updateDocType() : handleAdd()"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getDocTypes,
  addDocType,
  delDocType,
  getDocTypeById,
  updateDocType,
} from "@/api/building";

export default {
  name: "doc_type",
  data() {
    return {
      edit: false,
      listLoading: true,
      total: 0,
      docTypeList: [],
      dialogVisible: false,
      addForm: {
        id: null,
        name: "",
      },
      addFormRules: {
        id: [{ required: true, message: "请输入类型ID", trigger: "blur" }],
        name: [{ required: true, message: "请输入类型名称", trigger: "blur" }],
      },
      listQuery: {
        page: 1,
        page_size: 20,
      },
    };
  },
  created() {
    this.getDocTypeList();
  },
  methods: {
    //获取资料类型列表
    getDocTypeList() {
      this.listLoading = true;
      getDocTypes(this.listQuery)
        .then((res) => {
          if (res) {
            this.total = res.length;
            this.docTypeList = res;
            this.listLoading = false;
          }
        })
        .catch((error) => {
          console.log(error.message);
          this.listLoading = false;
        });
    },
    //点击添加按钮，弹窗显示
    addDocType() {
      this.edit = false;
      this.dialogVisible = true;
    },
    //确定添加工程事件
    handleAdd() {
      this.$refs.addFormRef.validate((valid) => {
        if (!valid) {
          this.$message.error("请填写必填项");
          this.addForm = {};
          return;
        } else {
          addDocType(this.addForm)
            .then((res) => {
              if (res) {
                this.dialogVisible = false;
                this.$message.success("添加成功！");
                this.addForm = {};
                this.getDocTypeList();
              }
            })
            .catch((err) => {
              console.log(err);
            });
        }
      });
    },
    //回显信息，弹框显示
    handleEdit(info) {
      this.edit = true;
      getDocTypeById(info).then((res) => {
        this.addForm = res;
      });
      this.dialogVisible = true;
    },
    //更新信息
    updateDocType() {
      this.$refs.addFormRef.validate(async (valid) => {
        if (!valid) return;
        else {
          const res = await updateDocType(this.addForm);
          const { id } = res;
          if (id > 0) {
            this.$message.success("修改成功");
            this.dialogVisible = false;
            this.getDocTypeList();
          }
        }
      });
    },
    //删除
    async handleDelete(id) {
      const removeResult = await this.$confirm("是否确定删除？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).catch((err) => err);
      if (removeResult !== "confirm") {
        return this.$message.info("已取消删除");
      }
      delDocType(id)
        .then((res) => {
          this.$message.success("删除成功！");
          this.getDocTypeList();
        })
        .catch((err) => err);
    },
    //关闭弹窗事件
    handleCloseDialog() {
      this.addForm = {};
      this.dialogVisible = false;
      this.$refs.addFormRef.resetFields();
    },
  },
};
</script>
<style lang="scss" scoped>
</style>